package com.huali.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javafx.scene.chart.ScatterChart;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.LineNumberReader;
import java.sql.*;
import java.util.*;

public class BaseDao {

    //数据源对象

    private static DataSource dataSource =null;

    //静态代码块
    static{
        //配置文件对象
        Properties properties =new Properties();
        try {
            //读取配置文件
            properties.load(BaseDao.class.getClassLoader().getResourceAsStream("druid.properties"));
            //创建数据源对象
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

//    获取数据库链接对象
//    @return  connection

    private  static Connection getConnection() throws SQLException {

        Connection connection = dataSource.getConnection();
        return connection;

    }
    //通用数据库修改方法
    //形参：sq--SQL语句
    //     data--参数列表
    //  返回值 ：int
    public  static int executeUpdate(String sql,Object... data) throws SQLException {
        //数据库链接对象
        Connection conn = null;
        //Sql执行对象
        PreparedStatement ps= null;

        //返回值
        int res =0;
        try {
            //调用方法获取数据库链接对象
            conn = getConnection();
            //获取sql执行对象
            ps = conn.prepareStatement(sql);
            //进行属性赋值
            for (int i=0;i< data.length;i++){
                ps.setObject(i+1,data[i]);
            }
            //执行sql获取响应
            res =ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //调用释放资源方法
            close(conn,ps,null);
        }
        return res;

    }


    public  static List<Map<String,Object>>excuteQuery(String sql, Object... data) {
        //数据库连接对象
        Connection conn = null;
        //Sql执行duix
        PreparedStatement ps = null;
        //返回结果集对象
        ResultSet rs = null;

        //返回结果
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            //获取数据库连接对象
            conn = getConnection();
            //sql执行对象
            ps = conn.prepareStatement(sql);

            for (int i = 0; i < data.length; i++) {
                ps.setObject(i + 1, data[i]);
            }
            //执行sql 返回
            rs = ps.executeQuery();
            //获取数据字段列数
            int count = rs.getMetaData().getColumnCount();

            while (rs.next()) {
                //创建存放一条数据对象
                Map<String, Object> map = new HashMap<>();
                for (int i = 1; i < count; i++) {
                    //获取字段名
                    String name = rs.getMetaData().getColumnLabel(i);
                    //获取值
                    Object values = rs.getObject(i);
                    //将名字和值存放到map中
                    map.put(name, values);
                }
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn, ps, rs);
        }


        return list;

    }


    private  static void  close(Connection conn, PreparedStatement ps, ResultSet rs){
        //inn
        if (conn != null) {
           try{
               conn.close();
           }catch (SQLException e){
               e.printStackTrace();
           }
        }
        if (ps != null) {
            try{
                ps.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try{
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

    }

    //ctrl +alt+l

}
